<?php

$date = date('Y-m-d');
require ('../plugins/SimpleImage.php');

if (isset($_GET['success']) && ($_GET['success'] == "1")) {

	$o .= '<div class="alert alert-success">
			Proses tambah berita berhasil
			</div>';
			
} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses tambah berita gagal
		   </div>';
} elseif (isset($_GET['upload']) && ($_GET['upload'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses upload gambar gagal
		   </div>';
} elseif (isset($_GET['upload']) && ($_GET['upload'] == "1")) {
	
	$o .= '<div class="alert alert-error">
			Proses upload gambar gagal nich
		   </div>';
}
	
echo $o;

?>
<script type="text/javascript" src="../assets/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "simple"
	});
</script>
<form class="form-horizontal" ENCTYPE="multipart/form-data" id="registerHere" method='post' action='?page=addberita&action=add'>
 <fieldset>
	<legend>TAMBAH BERITA</legend>
	<div class="control-group">
	  <label class="control-label" for="input01">Judul</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="judul" name="judul" rel="popover" data-content="Masukkan judul berita." data-original-title="Judul Berita">
	  </div>
	</div>

	<div class="control-group">
	<label class="control-label" for="input01">Tanggal</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" value="<?php echo $date; ?>" readonly=readonly id="tanggal" name="tanggal" rel="popover" data-content="Masukkan tanggal" data-original-title="Tanggal">
	  </div>
	</div>

	<div class="control-group">
	  <label class="control-label" for="input01">Gambar</label>
	  <div class="controls">
		<input type="file" class="input-file" id="image" name="image" rel="popover" data-content="Masukkan gambar." data-original-title="Gambar">
	  </div>
	</div>
	
	<div class="control-group">
	<label class="control-label" for="input01">Isi</label>
	  <div class="controls">
		<textarea id="elm1" name="isi" rows="15" cols="80" style="width: 80%"></textarea>
	  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01"></label>
		  <div class="controls">
		   <button type="submit" class="btn btn-success" >Tambah Berita</button>
		  </div>
	</div>
	  </fieldset>
</form>

<a href="?page=berita"><i class=" icon-arrow-left"></i> kembali</a>
<?php

if ($_GET['action'] == "add") {
	
	// standarisasi ukuran foto yang di upload
	$dim  = array('W'=>300, 'H'=>250);
	$uploaddir 	= "../file/berita/$picName";
	$picName 	= basename($_FILES['image']['name']);
	$username	= $_SESSION['username'];
	
	
	if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		
		if((copy($_FILES['image']['tmp_name'], $uploaddir.$picName))) {
			
			$imgType = IMAGETYPE_JPEG;
			$imgDms = getimagesize($_FILES['image']['tmp_name']);
			if ($imgDms[0] > $dim['W']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToWidth($dim['W']);
				$image->save($uploaddir.$picName, $imgType);
			} elseif ($imgDms[1] > $dim['H']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToHeight($dim['H']);
				$image->save($uploaddir.$picName, $imgType);
			}
		} else {
			echo '
				<script language="javascript">
				document.location="?page=addberita&upload=0";
				</script>';
		}	
			
				$db = new Berita();
				$berita = array ('judul'=>$_POST['judul'], 
								 'tgl'=>$_POST['tanggal'], 
								 'isi'=> htmlentities(mysql_real_escape_string($_POST['isi'])),
								 'gambar'=> $picName, 
								 'nama'=> $_SESSION['username']);

				$addberita = $db->tambah("berita", $berita);

				if ($berita) {
					?>
						<script language="javascript">
						document.location="?page=addberita&success=1";
						</script>
					<?php
					
				} else {
					?>
						<script language="javascript">
						document.location="?page=addberita&success=0";
						</script>
					<?php	
				}
	} else {
		echo '
			<script language="javascript">
			document.location="?page=addberita&upload=1";
			</script>';
	}	
}